﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
//载入NPOI类库
using NPOI.SS.UserModel;
using NPOI.HSSF.UserModel;
using NPOI.XSSF.UserModel;
using System.IO;

namespace PaySlips {
    public partial class Output : System.Web.UI.Page {
        protected void Page_Load(object sender, EventArgs e) {
            Response.Write("CodePlex");
            //HSSFWorkbook hwk = new HSSFWorkbook();
            XSSFWorkbook hwk = new XSSFWorkbook();
            IFont font = hwk.CreateFont();
            font.IsBold = true;
            //font.FontHeight = 9;
            ICellStyle style = hwk.CreateCellStyle();
            style.Alignment = HorizontalAlignment.Center;
            //style.VerticalAlignment = VerticalAlignment.Center;
            //XSSFCellStyle xcellstyle = new XSSFCellStyle();
            style.SetFont(font);

            //xcellstyle.SetFont(font);
            ISheet sheet = hwk.CreateSheet("表1");
            sheet.DefaultRowHeight = 20 * 20;   //设置表格默认行高
            sheet.SetColumnWidth(0, 12 * 256);  //列宽：使用填写的值 * 256（tip数），得出实际列宽为【填写的值】
            sheet.SetColumnWidth(1, 18 * 256);  //列宽：使用填写的值 * 256（tip数），得出实际列宽为【填写的值】

            IRow row = sheet.CreateRow(0);

            ICell cell = row.CreateCell(0);
            cell.CellStyle = style;
            //row.Height = 22 * 20;       //设备行高：使用填写的值 * 20（tip数），得出实际行高为【填写的值】

            //row.RowStyle = style;
            cell.SetCellValue("helloworld");
            row.CreateCell(1).SetCellValue(DateTime.Now.ToString());
            row.Cells[1].CellStyle = style;
            row.CreateCell(2).SetCellValue(33.33);
            row.CreateCell(3).SetCellFormula("C1*10");
            row.CreateCell(4).SetCellFormula("sum(C1:D1)");

            IRow row2 = sheet.CreateRow(1);
            IFont font2 = hwk.CreateFont();
            font2.IsBold = true;

            ICellStyle style2 = hwk.CreateCellStyle();
            font2.FontHeight = 14;   //字体大小：与数值字号对应
            style2.SetFont(font2);
            sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(1, 2, 0, 5));

            row2.CreateCell(0).SetCellValue("好吧，合并单元格的值");      //合并前、合并后都可以
            row2.Cells[0].CellStyle = style2;
            //sheet.CreateRow(0).CreateCell(1).SetCellValue(DateTime.Now.ToString());

            string filename = Server.MapPath("~/") + string.Format("{0}.xlsx", DateTime.Now.ToString("yyyyMMddHHmmss"));
            FileStream fs = new FileStream(filename, FileMode.Create);
            hwk.Write(fs);
            hwk.Close();
            fs.Close();
            fs.Dispose();

            //HSSFWorkbook hwk = new HSSFWorkbook();
            //MemoryStream ms = new MemoryStream();
            //hwk.CreateSheet("表1");
            //hwk.Write(ms);
            ////Response.AppendHeader("Content-Disposition", string.Format("attchment;filename=EmptyWorkBook.xls"));
            //Response.ContentType = "application/vnd.ms-excel";
            //Response.AddHeader("Content-Disposition", string.Format("attachment; filename=EmptyWorkbook.xls"));
            //Response.BinaryWrite(ms.ToArray());

            //hwk = null;
            //ms.Close();
            //ms.Dispose();
        }
    }
}